package com.flxz.user.controller;

import com.flxz.common.annotation.Log;
import com.flxz.common.core.controller.BaseController;
import com.flxz.common.core.domain.AjaxResult;
import com.flxz.common.enums.BusinessType;
import com.flxz.user.service.IUserProfileService;
import com.flxz.user.domain.vo.UserProfileVO;
import com.flxz.user.domain.vo.SignInInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 用户个人信息Controller
 * 
 * @author flxz
 * @date 2024-01-15
 */
@RestController
@RequestMapping("/user/profile")
public class UserProfileController extends BaseController
{
    @Autowired
    private IUserProfileService userProfileService;

    /**
     * 获取用户个人信息
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:profile:query')")
    @GetMapping
    public AjaxResult getUserProfile()
    {
        Long userId = getUserId();
        UserProfileVO profile = userProfileService.getUserProfile(userId);
        return success(profile);
    }

    /**
     * 更新用户个人信息
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:profile:edit')")
    @Log(title = "用户个人信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult updateUserProfile(@RequestBody UserProfileVO profileVO)
    {
        Long userId = getUserId();
        profileVO.setUserId(userId);
        return toAjax(userProfileService.updateUserProfile(profileVO));
    }

    /**
     * 用户签到
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:signin:add')")
    @Log(title = "用户签到", businessType = BusinessType.INSERT)
    @PostMapping("/sign-in")
    public AjaxResult signIn()
    {
        Long userId = getUserId();
        return toAjax(userProfileService.signIn(userId));
    }

    /**
     * 用户签到（简化接口）
     */
    @PostMapping("/checkin")
    public AjaxResult checkin()
    {
        Long userId = getUserId();
        return toAjax(userProfileService.signIn(userId));
    }

    /**
     * 获取签到信息
     */
    @PreAuthorize("@ss.hasRole('common') or @ss.hasPermi('user:signin:query')")
    @GetMapping("/sign-in/info")
    public AjaxResult getSignInInfo()
    {
        Long userId = getUserId();
        SignInInfoVO signInInfo = userProfileService.getSignInInfo(userId);
        return success(signInInfo);
    }
}
